package cn.myschool.dao;

import cn.myschool.util.DBUtil;
import cn.myschool.entity.Subject;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */
//处理s_subject相关的操作
public class SubjectDao {
    public int add(Subject s) {
        Connection connection = null;
        PreparedStatement st = null;
        try {
            connection = DBUtil.getConnection();

            String sql = "insert into s_subject(subjectname,classhour,gradeid) values(?,?,?)";
            System.out.println(sql);
            st = connection.prepareStatement(sql);
            st.setString(1, s.getSubjectname());
            st.setInt(2, s.getClasshour());
            st.setLong(3, s.getGradeid());

            return st.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(null, st, connection);
        }
        return 0;
    }

    public int update(Subject s) {
        return 0;
    }

    public int del(long id) {
        return 0;
    }

    //主键找对象
    public Subject get(long id) {
        String sql = "select id,subjectname,classhour,gradeid from s_subject where id=?";
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Subject s = null;
//        Subject s=new Subject();
        try {
            con = DBUtil.getConnection();
            ps = con.prepareStatement(sql);
            ps.setLong(1, id);
            rs = ps.executeQuery();
            if (rs.next()) {
                s = new Subject(rs.getLong("id"), rs.getString("subjectname"), rs.getInt("classhour"), rs.getLong("gradeid"));
            }
        } catch (SQLException e) {
            DBUtil.handlerEx(e);
            //e.printStackTrace();
        } finally {
            DBUtil.close(rs, ps, con);
        }
        return s;
    }

    //查询全部对象,找不到返回空集合
    public List<Subject> findALL() {
        String sql = "select * from s_subject order by id asc";
        List<Subject> datas = new ArrayList<>();
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            con = DBUtil.getConnection();
            ps = con.prepareStatement(sql);

            rs = ps.executeQuery();
            while (rs.next()) {
                datas.add(new Subject(rs.getLong("id"), rs.getString("subjectname"), rs.getInt("classhour"), rs.getLong("gradeid")));
            }
        } catch (SQLException e) {
            DBUtil.handlerEx(e);
            //e.printStackTrace();
        } finally {
            DBUtil.close(rs, ps, con);
        }
        return datas;
    }
    public List<Subject> search(int pageNo,int pageSize) {
        String sql = "select * from s_subject order by id asc";
        List<Subject> datas = new ArrayList<>();
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            con = DBUtil.getConnection();
            ps = con.prepareStatement(sql);

            rs = ps.executeQuery();
            while (rs.next()) {
                datas.add(new Subject(rs.getLong("id"), rs.getString("subjectname"), rs.getInt("classhour"), rs.getLong("gradeid")));
            }
        } catch (SQLException e) {
            DBUtil.handlerEx(e);
            //e.printStackTrace();
        } finally {
            DBUtil.close(rs, ps, con);
        }
        return datas;
    }
}
